package com.motong.gongdan.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.motong.gongdan.config.ResponseData;
import com.motong.gongdan.entity.Departments;
import com.motong.gongdan.entity.User;
import com.motong.gongdan.mapper.DepartmentsMapper;
import com.motong.gongdan.util.LoginUtil;
import com.motong.gongdan.util.StringUtil;
import com.motong.gongdan.vo.resp.NameVo;
import com.motong.gongdan.vo.system.common.query.BooleanQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentsService extends ServiceImpl<DepartmentsMapper, Departments> {


    @Resource
    DepartmentsService departmentsService;
    @Resource
    UserService userService;

    public ResponseData<List<NameVo>> getListResponseData(NameVo vo, BooleanQuery booleanQuery) {
        List<NameVo> dataList = new ArrayList<>();
        //添加所有人
        if (booleanQuery.isGetAllLabel()) {
            NameVo allVo = new NameVo().setId(-1).setName("所有人").setType("user").setNodeId("user-1").setTypeFlag("all").setNodePid("BaseTreeEntity_null").setListDown(Collections.emptyList());
            dataList.add(allVo);
        }
        List<Departments> list = departmentsService.list(new QueryWrapper<Departments>().eq("del_flag", 0).like(StringUtil.isNotBlank(vo.getScreen()), "name", vo.getScreen()));
        List<Integer> haveDepUser = new ArrayList<>();
        if (Objects.nonNull(list) && !CollectionUtils.isEmpty(list)) {
            list.forEach(i -> {
                NameVo depVo = new NameVo().setId(i.getId()).setName(i.getName()).setType("dep").setNodePid("BaseTreeEntity_null");
                depVo.setNodeId(depVo.getType() + depVo.getId());
                if (StringUtil.isNotBlank(i.getIds())) {
                    List<User> listUser = userService.listByIds(Arrays.asList(i.getIds().split(",")));
                    if (!CollectionUtils.isEmpty(listUser)) {
                        List<NameVo> userList = listUser.stream().map(u -> {
                            NameVo userVo = new NameVo().setName(u.getName()).setId(u.getId()).setListDown(Collections.emptyList()).setNodePid(depVo.getNodeId()).setType("user");
                            userVo.setNodeId(userVo.getId().toString());
                            haveDepUser.add(u.getId());
                            return userVo;
                        }).collect(Collectors.toList());
                        depVo.setListDown(userList);
                    }
                } else {
                    depVo.setListDown(Collections.emptyList());
                }
                dataList.add(depVo);
            });
        }
        QueryWrapper<NameVo> noDepQw = new QueryWrapper<NameVo>().eq("factory_id", LoginUtil.getLoginUserFactory())
                .notIn(!CollectionUtils.isEmpty(haveDepUser), "id", haveDepUser)
                .like(StringUtil.isNotBlank(vo.getScreen()), "name", vo.getScreen());
        List<NameVo> nameVos = userService.getBaseMapper().getList(noDepQw);
        if (!CollectionUtils.isEmpty(nameVos)) {
            nameVos.forEach(i -> {
                i.setNodePid("BaseTreeEntity_null").setType("user").setListDown(Collections.emptyList());
                i.setNodeId(i.getType() + i.getId());
            });
            dataList.addAll(nameVos);
        }

        return ResponseData.success(dataList);
    }


}
